package com.sanjiahantang.youjiarole.module.project.views.impl;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.sanjiahantang.uilibrary.adapter.listview.BaseAdapterHelper;
import com.sanjiahantang.uilibrary.adapter.listview.QuickAdapter;
import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.youjiarole.R;
import com.sanjiahantang.youjiarole.base.BaseMvpFragment;
import com.sanjiahantang.youjiarole.base.BaseView;
import com.sanjiahantang.youjiarole.config.ConfigValue;
import com.sanjiahantang.youjiarole.config.SPConfig;
import com.sanjiahantang.youjiarole.module.decorate.views.impl.DecorateDetailActivity;
import com.sanjiahantang.youjiarole.module.me.scheme.beans.MeSchemeListBean;
import com.sanjiahantang.youjiarole.module.me.scheme.views.impl.MeSchemeActivity;
import com.sanjiahantang.youjiarole.module.project.presenters.ProjectPresenter;
import com.sanjiahantang.youjiarole.module.project.views.ProjectView;
import com.sanjiahantang.youjiarole.utils.CheckUtil;
import com.sanjiahantang.youjiarole.utils.SPUtils;

import java.util.List;

/**
 * Created by zy on 2017/10/29.
 */

public class ProjectListFragment extends BaseMvpFragment<ProjectView,ProjectPresenter>
        implements ProjectView, View.OnClickListener,BaseView.OnLoadListener {

    private View mView,vsNetworkError;
    private RadioButton rbStateDesign,rbStateConstruct,rbStateDone;
    private ListView listView;
    private QuickAdapter<MeSchemeListBean.Item> projectListAdapter;
    private int pageType = 1;
    private RadioButton[] radioButtons;
    private String currentPhone;
    private String currentRoleId;

    @Override
    public void showProgress() {
        if (context != null)context.showProgress(context);
    }

    @Override
    public void hideProgress() {
        if (context != null)context.hideProgress();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_project_main, container, false);
        return mView;
    }

    @Override
    public ProjectPresenter initPresenter() {
        return new ProjectPresenter();
    }

    @Override
    protected void initView() {
        rbStateDesign = (RadioButton) mView.findViewById(R.id.rbStateDesign);
        rbStateConstruct = (RadioButton) mView.findViewById(R.id.rbStateConstruct);
        rbStateDone = (RadioButton) mView.findViewById(R.id.rbStateDone);
        radioButtons = new RadioButton[]{
                rbStateDesign,
                rbStateConstruct,
                rbStateDone
        };
        listView = (ListView) mView.findViewById(R.id.listView);
        vsNetworkError = mView.findViewById(R.id.vsNetworkError);

        rbStateDesign.setOnClickListener(this);
        rbStateConstruct.setOnClickListener(this);
        rbStateDone.setOnClickListener(this);
        mView.findViewById(R.id.rbStateDesign).setOnClickListener(this);
        mView.findViewById(R.id.rbStateConstruct).setOnClickListener(this);
        mView.findViewById(R.id.rbStateDone).setOnClickListener(this);
        currentPhone = SPUtils.get(context, SPConfig.PHONE,"").toString();
        currentRoleId = SPUtils.get(context, SPConfig.ROLE_ID,"").toString();


        projectListAdapter = new QuickAdapter<MeSchemeListBean.Item>(context,R.layout.item_project) {
            @Override
            protected void convert(BaseAdapterHelper helper, final MeSchemeListBean.Item item) {
                helper.setText(R.id.txtApartName,item.getApartmentName());
                helper.setText(R.id.txtName,item.getUserName());
                String city = "";
                if (item.getAddress() != null && item.getAddress().contains(" ")){
                    String[] arr = item.getAddress().split(" ");
                    if (arr.length >= 2){
                        city = arr[1].replace("市","");
                    }else if(arr.length > 0){
                        city = arr[0];
                    }else{
                        city = item.getAddress();
                    }
                }else if(item.getAddress() != null){
                    city = item.getAddress();
                }
                helper.setText(R.id.txtCity,city);
                helper.getView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (pageType == 1){
                            Intent intent = new Intent(context,MeSchemeActivity.class);
                            intent.putExtra(KEY_ID,String.valueOf(item.getId()));
                            startActivity(intent);
                        } else {
                            Intent intent = new Intent(context,DecorateDetailActivity.class);
                            intent.putExtra(KEY_ID,String.valueOf(item.getId()));
                            startActivity(intent);
                        }

                    }
                });

                if (helper.getPosition() == projectListAdapter.getCount() - 1){
                    helper.setVisible(R.id.view_Line,false);
                }else{
                    helper.setVisible(R.id.view_Line,true);
                }
                helper.setVisible(R.id.txtSubcontract,false);
                if(pageType != 1){
                    if (item.getIsSubcontractProject() == 1){
                        helper.setVisible(R.id.txtSubcontract,true);
                        if (currentPhone.equals(item.getProjectPhone()) && !TextUtils.isEmpty(item.getProjectRoleId())){
                            helper.setText(R.id.txtSubcontract,"来自： " + item.getProjectRoleName() + "(" + item.getProjectRoleId() + ")");
                        }else if(currentPhone.equals(item.getProjectRoleId()) && !TextUtils.isEmpty(item.getProjectPhone())){
                            helper.setText(R.id.txtSubcontract,"分包： " + item.getProjectName() + "(" + item.getProjectPhone() + ")");
                        }else {
                            helper.setVisible(R.id.txtSubcontract,false);
                        }
                    }
                }else {
                    if (item.getIsSubcontract() == 1){
                        helper.setVisible(R.id.txtSubcontract,true);
                        if (currentPhone.equals(item.getDesignPhone()) && !TextUtils.isEmpty(item.getDesignRoleId())){
                            helper.setText(R.id.txtSubcontract,"来自： " + item.getDesignRoleName() + "(" + item.getDesignRoleId() + ")");
                        }else if(currentPhone.equals(item.getDesignRoleId()) && !TextUtils.isEmpty(item.getDesignPhone())){
                            helper.setText(R.id.txtSubcontract,"分包： " + item.getDesignName() + "(" + item.getDesignPhone() + ")");
                        }else {
                            helper.setVisible(R.id.txtSubcontract,false);
                        }
                    }
                }
            }
        };

        listView.setAdapter(projectListAdapter);

        TextView title = (TextView) mView.findViewById(R.id.title);
        title.setText("项目管理");


    }

    @Override
    protected void initData() {

    }


    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.rbStateDesign:case R.id.vStateDesign:
                pageType = 1;
                checkRadioButton(rbStateDesign);
                break;
            case R.id.rbStateConstruct:case R.id.vStateConstruct:
                pageType = 2;
                checkRadioButton(rbStateConstruct);
                break;
            case R.id.rbStateDone:case R.id.vStateDone:
                pageType = 3;
                checkRadioButton(rbStateDone);
                break;
        }
    }

    private void checkRadioButton(RadioButton button) {
        rbStateDesign.setChecked(false);
        rbStateConstruct.setChecked(false);
        rbStateDone.setChecked(false);
        button.setChecked(true);
        if (Utils.NO_NETWORK_STATE == Utils.isNetworkAvailable(context)) {
            onNetError(true);
            return;
        }else{
            onNetError(false);
        }
        presenter.loadProjectList(context,pageType,this);
    }

    public void onNetError(boolean b) {
        if (b){
            vsNetworkError.setVisibility(View.VISIBLE);
            vsNetworkError.findViewById(R.id.btnReloadNetwork).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    checkRadioButton(radioButtons[pageType - 1]);
                }
            });
        }else{
            vsNetworkError.setVisibility(View.GONE);
        }
    }

    @Override
    public void onLoad(Object content) {
        if (content != null){
            List<MeSchemeListBean.Item> list = (List<MeSchemeListBean.Item>) content;
            projectListAdapter.clear();
            projectListAdapter.addAll(list);
        }

    }

    @Override
    public void onResume() {
        super.onResume();
        if (ConfigValue.param != null && ConfigValue.param instanceof Integer){
            pageType = (int) ConfigValue.param;
            ConfigValue.param = null;
        }

        CheckUtil.isRoleValid(context, new OnLoadListener() {
            @Override
            public void onLoad(Object content) {
                checkRadioButton(radioButtons[pageType - 1]);
            }
        });

    }
}
